package cn.cs.common;

import com.alibaba.fastjson.JSONObject;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @title:
 * @description:读取配置参数
 */
public class ReadparameterUtils {
	String fileName="parameter.properties";
//	File f = new File(rootPath+ "/parameter.properties");
//	File f = new File("D:\\workspace\\cs\\cs-utils\\src\\main\\java\\config\\parameter.properties");

	public static void main(String[] args) throws IOException {
		ReadparameterUtils readparameterUtils = ReadparameterUtils.getInstance();
		System.out.println(readparameterUtils.getVal("proportion_city"));
//		System.out.println(readparameterUtils.prokey());
	}

	/**
	 * @description:读取pro
	 * @return
	 * @throws IOException
	 */
	private  Properties getp() throws IOException {
		Properties p = new Properties();
		InputStreamReader inputStream = new InputStreamReader(PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName), "UTF-8");
		p.load(inputStream);
		inputStream.close();
		return p;
	}

	public  String getVal(String key) throws IOException {
		Properties p = getp();
		return  p.getProperty(key);
	}

	/**
	 * @param params  返回map 类型
	 * @description: 读取Properties设定参数
	 * @throws IOException
	 */
	public Map readp(String[] params) throws IOException{
		Map map = new HashMap();
		Properties p=getp();
		for(String pr :params){
			map.put(pr, p.getProperty(pr));
		}
		return map;
	}

	/**
	 * @param params 返回JSON
	 * @description: 读取Properties设定参数
	 * @throws IOException
	 */
	public String readj(String[] params) throws IOException{
		Map map = new HashMap();
		Properties p=getp();
		for(String pr :params){
			map.put(pr, p.getProperty(pr));
		}
		String jsonObject =JSONObject.toJSONString(map);
		return jsonObject;
	}

	/**
	 * @description:修改Properties内的参数
	 * @param map
	 * @throws IOException
	 */
	public String writep(Map map) throws IOException{
		Properties p=getp();
		//读取所有参数值
		Map map2 = new HashMap();
		for(Enumeration e=p.propertyNames(); e.hasMoreElements();){
			String key=(String) e.nextElement();
			map2.put(key, p.getProperty(key));
		}
		FileOutputStream fos=new FileOutputStream(PropertiesUtils.class.getClassLoader().getResource(fileName).getFile());
		//修改参数值
		Set set= map.keySet();
		Iterator iterator= set.iterator();
		String keyp=null;
		while(iterator.hasNext()){
			keyp=iterator.next().toString();
			map2.put(keyp, map.get(keyp).toString());
		}
		//重新写入配置文件
		Set set1= map2.keySet();
		Iterator iterator1= set.iterator();
		String keyp1=null;
		while(iterator1.hasNext()){
			keyp1=iterator1.next().toString();
			p.setProperty(keyp1, map2.get(keyp1).toString());
		}
		p.store(fos,null);
		fos.flush();
		fos.close();
		return "SUCCESS";
	}


	/**
	 * @description:遍历pro内部所有 key与value
	 * @throws IOException
	 */
	public String prokey() throws IOException{
		Map map = new HashMap();
		Properties p=getp();
		Enumeration enu=getp().propertyNames();
		List list = new ArrayList();
		int i=0;
		while(enu.hasMoreElements()){
			String key = (String)enu.nextElement();
			map.put(key, p.get(key));
		}
		String jsonObject =JSONObject.toJSONString(map);
		return jsonObject;
	}


	/**
	 * @description:实例化一下
	 * @return
	 */
	public static ReadparameterUtils readp=null;

	public static ReadparameterUtils getInstance() {
		if (readp==null)
			readp=new ReadparameterUtils();
		return readp;
	}
}
